Page({
  leftMove:0,
  rightMove:0,
  /**
   * 页面的初始数据
   */
  data: {
    actionSheetHidden: true,
    actionSheetItems: [],
    title:"",
    desc:"",
    voice:0,
    leftAnimationData: {},
    rightAnimationData: {},
    leftTime:0,
    rightTime:0,
    src: 'http://dx.sc.chinaz.com/Files/DownLoad/sound1/201509/6339.mp3',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // 使用 wx.createAudioContext 获取 audio 上下文 context
    this.audioCtx = wx.createAudioContext('myAudio')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var configs = wx.getStorageSync("configs", configs); //同步获取本地缓存数据;
    var actionSheetItems = [];
    var first = true;
    for( var i in configs){
        var config = configs[i];
        if(config.state){
          if(first){
            var desc = config.desc.replace(/@/g,config.time + "秒");//replace函数只是查找第一个然后进行替换
            this.setData({title:config.name,desc:desc,leftTime:config.time,rightTime:config.time,voice:config.voice});//判断标题是否存在，然后存储数据
            first = false;
          }
          actionSheetItems.push({ name:config.name,id:config.id});
        }    
    }
    this.setData({ actionSheetItems: actionSheetItems });

  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  actionSheetTap: function (e) {
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },
  actionSheetChange: function (e) {
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },
  bindItemTap:function(e){
    this.leftStop();  //在切换标题时停掉计时器
    this.rightStop();
    var id = e.target.id;//获取点击时的ID
    var configs = wx.getStorageSync('configs');//从本地缓存中同步获取数据
    var config = configs[id];
    var desc = config.desc.replace(/@/g, config.time + "秒");//replace函数只是查找第一个然后进行替换
    this.setData({ title: config.name, desc: desc, actionSheetHidden: true, leftTime: config.time, rightTime: config.time, voice: config.voice});//判断标题是否存在，然后存储数据
  },
  leftStop:function(){
    clearInterval(this.leftInterval);
    this.leftInterval = 0;
    this.audioPause();
  },
  leftStart:function(){
    this.rightStop();//停掉右边的定时器
    if (this.leftInterval && this.leftInterval != 0) {//判断定时器是否存在
      this.leftStop();
      return;
    }
    var animation = wx.createAnimation({  //创建动画
      duration: 1000,
      timingFunction: "ease",
    })
    animation.rotate(this.leftMove += 100).step();
    this.setData({
      leftAnimationData: animation.export()
    })
    var page = this; // 将作用域扩展到下一个小子域
    var leftInterval =  setInterval(function(){
      if (page.data.leftTime <= 0) {
        page.leftStop();
        return;
      }
      if (page.data.leftTime <= page.data.voice){
        page.audioPlay();
      }
        animation.rotate(page.leftMove += 100).step();
        page.setData({
          leftAnimationData: animation.export()
        })
        page.setData({ leftTime: page.data.leftTime-1});
    },1000);
    this.leftInterval = leftInterval;

  },
  rightStop: function () {
    clearInterval(this.rightInterval);
    this.rightInterval = 0;
    this.audioPause();
  },
  rightStart: function () {
    this.leftStop();
    if (this.rightInterval && this.rightInterval != 0) {
      this.rightStop();
      return;
    }
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: "ease",
    })
    animation.rotate(this.rightMove += 100).step();
    console.log('aaa');
    this.setData({
      rightAnimationData: animation.export()
    })
    var page = this;
    var rightInterval = setInterval(function () {
      if (page.data.rightTime <= 0){
        page.rightStop();
        return; //返回作用域初始化
      }
      if (page.data.rightTime <= page.data.voice) {
        page.audioPlay();
      }
      animation.rotate(page.rightMove += 100).step();
      page.setData({
        rightAnimationData: animation.export()
      });
      page.setData({ rightTime: page.data.rightTime - 1 });
    }, 1000);
    this.rightInterval = rightInterval;
  },
  audioPlay: function () {
    this.audioCtx.play()
  },
  audioPause: function () {
    this.audioCtx.pause()
  }


})
